from google.appengine.ext import webapp
from DataClass.WebsiteRec import WebsiteRec
from google.appengine.api import users
from google.appengine.ext.webapp import template
from AdminBase import AdminBase

import os
import cgi

class EditWebSiteHandler(AdminBase):
    def GetTemplateFileName(self):
        return 'Pages/editwebsite.html'
        
    def GetBreadcrumbList(self):
        return [['main', 'admin'],['websites', 'editwebsites'],['website']]
    
    def GetTemplateValuesDict(self, template_values, user):
        retval = template_values

        querystring=cgi.FieldStorage()
        sitename=querystring.getvalue('sitename')
        Website=[]
    
        if sitename:
            Website = WebsiteRec.GetWebsiteBySiteName(sitename)

        if Website:
            retval['sitename'] = Website.sitename
            retval['Website'] = Website
            retval['titletext'] = 'Edit Web Site'
            retval['buttontext'] = 'Update'
            retval['is_update'] = True
        else:
            retval['sitename'] = ""
            retval['Website'] = WebsiteRec()
            retval['titletext'] = 'Create New Web Site'
            retval['buttontext'] = 'Create'
            retval['is_update'] = False
    
        return retval

    def DoPost(self, user):
        sitename = self.request.get('sitename')
        Website = WebsiteRec.GetWebsiteBySiteName(sitename)
        if (self.request.get('mainupdate')):
            if not Website:
                Website = WebsiteRec()
                Website.owneremail = user.email()
     
            Website.sitename = self.request.get('sitename')
            Website.prefix = self.request.get('prefix')
            Website.sitebase = self.request.get('sitebase')
            Website.put()
            self.redirect('/editwebsites')
        elif (self.request.get('newadmin')):
            if Website:
                newadminemail = self.request.get('adminemail')
                Website.adminuseremails = Website.adminuseremails + [newadminemail]
                Website.put()
        elif (self.request.get('newuser')):
            if Website:
                newuseremail = self.request.get('useremail')
                Website.useremails = Website.useremails + [newuseremail]
                Website.put()
            
        self.redirect('/editwebsites')
